package com.woniuxy.joylifecenter.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.entity.CenterAuth;
import com.woniuxy.joylifecenter.exception.CenterException;
import com.woniuxy.joylifecenter.service.AuthService;
import com.woniuxy.joylifecenter.vo.VO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@Api(value = "", description = "权限管理Controller")
@Slf4j
public class Center_Auth_Controller {

    @Autowired
    AuthService authService;

    /**
     * @return com.woniuxy.joylifecenter.vo.VO
     * @Author ArmWatt
     * @Date 2020/5/8
     * @Param [centerAuthName, centerAuthUrls]
     * @Description 添加权限
     **/
    @ApiOperation(value = "/addAuth", notes = "添加权限")
    @RequestMapping("/addAuth")
    @ResponseBody
    public VO addAuth(@RequestBody HashMap<String, String> map) {

        CenterAuth centerAuth = new CenterAuth();
        centerAuth.setCenterAuthid(UUID.randomUUID().toString().replaceAll("-", ""));
        centerAuth.setCenterAuthName(map.get("centerAuthName"));
        centerAuth.setCenterAuthUrls(map.get("centerAuthUrls"));
        VO vo = new VO();
        int count = 0;
        try {
            count = authService.addAuth(centerAuth);
            vo.setMsg("成功添加权限:");
        } catch (CenterException e) {
            vo.setMsg(e.getMessage());
        }
        vo.setCount(count);
        vo.setCode(0);
        vo.setData(null);
        return vo;

    }

    /**
     * @return com.woniuxy.joylifecenter.vo.VO
     * @Author ArmWatt
     * @Date 2020/5/8
     * @Param [centerAuthName, centerAuthUrls]
     * @Description 编辑权限
     **/
    @ApiOperation(value = "/editAuth", notes = "编辑权限")
    @RequestMapping("/editAuth")
    @ResponseBody
    public VO editAuth(@RequestBody CenterAuth centerAuth) {
        VO vo = new VO();
        centerAuth.setCenterAuthName(centerAuth.getCenterAuthName());
        centerAuth.setCenterAuthUrls(centerAuth.getCenterAuthUrls());
        int count = 0;
        try {
            count = authService.editAuth(centerAuth);
            vo.setMsg("本次更新了:");
        } catch (CenterException e) {
            vo.setMsg(e.getMessage());
        }
        vo.setCount(count);
        vo.setCode(0);
        vo.setData(null);
        return vo;
    }

    /**
     * @return com.woniuxy.joylifecenter.vo.VO
     * @Author ArmWatt
     * @Date 2020/5/8
     * @Param [authId]
     * @Description 删除单个权限
     **/
    @ApiOperation(value = "/deleteAuth", notes = "删除权限")
    @RequestMapping("/deleteAuth")
    @ResponseBody
    public VO deleteAuth(@RequestBody String authId) {
        VO vo = new VO();
        int count = 0;
        try {
            count = authService.deleteAuth(authId);
            vo.setMsg("已删除:");
        } catch (CenterException e) {
            vo.setMsg(e.getMessage());
        }
        vo.setCode(0);
        vo.setCount(count);
        vo.setData(null);
        return vo;
    }

    /**
     * @return com.woniuxy.joylifecenter.vo.VO
     * @Author ArmWatt
     * @Date 2020/5/8
     * @Param [authIds]
     * @Description 批量删除权限
     **/
    @ApiOperation(value = "/batchDelteAuth", notes = "批量删除权限")
    @RequestMapping("/batchDeleteAuth")
    @ResponseBody
    public VO batchDeleteAuth(@RequestBody List<String> authIds) {
        System.out.println("后端接收到的待批量删除数据：" + authIds);
        VO vo = new VO();
        int count = 0;
        try {
            count = authService.batchDeleteAuth(authIds);
            vo.setMsg("已批量删除:");
        } catch (CenterException e) {
            vo.setMsg(e.getMessage());
        }
        vo.setCode(0);
        vo.setCount(count);
        vo.setData(null);
        return vo;
    }

    /**
     * @return com.woniuxy.joylifecenter.vo.VO
     * @Author ArmWatt
     * @Date 2020/5/8
     * @Param [pageMap]
     * @Description 多条件模糊查询权限
     **/
    @ApiOperation(value = "/showAuthByCondition", notes = "多条件模糊查询权限")
    @RequestMapping("/showAuthByCondition")
    @ResponseBody
    public VO showAuthByCondition(@RequestBody HashMap<String, String> pageMap) {
        CenterAuth centerAuth = new CenterAuth();
        String jsonstr = pageMap.get("jsonstr");
        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> map = new HashMap<>();
        try {
            map = (HashMap<String, String>) mapper.readValue(jsonstr, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        centerAuth.setCenterAuthid(map.get("centerAuthid"));
        centerAuth.setCenterAuthName(map.get("centerAuthName"));
        centerAuth.setCenterAuthUrls(map.get("centerAuthUrls"));
        PageHelper.startPage(Integer.parseInt(pageMap.get("page")), Integer.parseInt(pageMap.get("limit")));
        List<CenterAuth> centerAuths = authService.queryAuthByCondition(centerAuth);
        PageInfo pageInfo = new PageInfo(centerAuths);
        List<PageInfo> pageInfos = new ArrayList<PageInfo>();
        pageInfos.add(pageInfo);
        VO vo = new VO();
        vo.setCode(0);
        vo.setMsg("");
        vo.setData(pageInfos);
        //此处需要从pageInfo获取总记录条数，不能直接用Service返回的集合大小
        vo.setCount(new Long(pageInfo.getTotal()).intValue());
        return vo;

    }

}
